﻿<?php include_once("connection.php"); // the connection to the database ?>
<div style="overflow:hidden; margin:1em;">
<h1>Thêm category mới</h1>
<?php if($_GET['submitted']) { 
	
	$newCategory = mysql_query("INSERT INTO category (cat_ten,cat_mota,cat_parentID,orderby) 
								VALUES ('".$_POST['cat_ten']."','".$_POST['cat_mota']."',".$_POST['cat_parentID'].",".$_POST['orderby'].")
								") or die(mysql_error());
	
/*	
	$db = Zend_Registry::get('db');
	$table = 'category';
	$data = array('cat_ten'=> $_POST['cat_ten'],
				  'cat_mota' => $_POST['cat_mota'],
				  'cat_parentID'  => $_POST['cat_parentID'],		
				  'orderby' => $_POST['orderby']
	);
	$db->insert($table, $data);							
	*/							
	?>
	<script type="text/javascript">	
		$(document).ready(function(){
			$.nyroModalRemove();
		});
	</script>	
	<?php
	
} else { 
//dau tien thuc hien viec nay :d
// extract the max order number: the new category will have this order number + 1
$rsMaxOrder = mysql_query("SELECT MAX(orderby) FROM category WHERE cat_parentID = ".$_GET['cat_parentID']) or die(mysql_error());
$results = mysql_fetch_array($rsMaxOrder);
$maxOrderby = $results[0];

?>

<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]."?submitted=true" ?>" name="add_new" id="add_new" class="nyroModal">
	<label for="cat_ten">Tên category:</label></br>
	<input type="text" value="" name="cat_ten" id="cat_ten" /></br>
	<label for="cat_mota">Mô tả:</label></br>
	<input type="text" value="" name="cat_mota" id="cat_mota" /></br>
	<input type="hidden" value="<?php echo $_GET['cat_parentID'] ?>" name="cat_parentID" id="cat_parentID" />
	<input type="hidden" value="<?php echo $maxOrderby+1 ?>" name="orderby" id="orderby" />
	<input type="submit" value="Add" id="form_submit" />
</form>
<script type="text/javascript">	
	$(document).ready(function(){
		$("#form_submit").click(function(e){
			e.preventDefault();
			if ( $("#cat_ten").val() != "" ) $("#add_new").submit();
			else alert("Hãy nhập tên category:");
		});
	});
</script>	 
<?php } ?>
</div>